﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace ChangDiTools.Demo {
    public class Interactive {
        [CommandMethod("AddPoly")]
        public void AddPoly()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            double width = 0;//初始化线宽
            short colorIndex = 0;
            int index = 2;
            ObjectId polyEntId=ObjectId.Null;
            PromptPointOptions ppo=new PromptPointOptions("");
            ppo.Message = "\n请输入第一个点<100,200>";
            ppo.AllowNone = true;
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status==PromptStatus.Cancel)
                return;
            Point3d ptStart;
            if (ppr.Status==PromptStatus.None)
                ptStart=new Point3d(100,200,0);
            else
                ptStart = ppr.Value;
            Point3d ptPrevious=ptStart;
            ppo.Message = "\n请输入下一个点或[线宽(W)/颜色(C)/完成(O)]<O>";
            ppo.Keywords.Add("W");
            ppo.Keywords.Add("C");
            ppo.Keywords.Add("O");
            ppo.Keywords.Default = "O";
            ppo.UseBasePoint = true;
            ppo.BasePoint = ptPrevious;
            ppo.AppendKeywordsToMessage = false;
            ppr = ed.GetPoint(ppo);
            while (ppr.Status==PromptStatus.OK||ppr.Status==PromptStatus.Keyword)
            {
                Point3d ptNext;
                if (ppr.Status==PromptStatus.Keyword)
                {
                    switch (ppr.StringResult)
                    {
                        case "W":
                            width = GetWidth(ed);
                            break;
                        case "C":
                            colorIndex = GetColorIndex(ed);
                            break;
                        case "O":
                            return;
                        default:
                            ed.WriteMessage("\n输入的关键字无效");
                            break;
                    }
                }
                else
                {
                    ptNext = ppr.Value;
                    if (index == 2)
                    {
                        Point2d pt1=new Point2d(ptPrevious[0],ptPrevious[1]);
                        Point2d pt2=new Point2d(ptNext[0],ptNext[1]);
                        Polyline pl=new Polyline();
                        pl.AddVertexAt(0,pt1,0,width,width);
                        pl.AddVertexAt(1,pt2,0,width,width);
                        pl.ColorIndex = colorIndex;
                        polyEntId = db.AddEntity(pl);
                    }
                    else
                    {
                        using (var trans=db.TransactionManager.StartOpenCloseTransaction())
                        {
                            Polyline pl=trans.GetObject(polyEntId,OpenMode.ForWrite) as Polyline;
                            if (pl!=null)
                            {
                                Point2d ptCurrent=new Point2d(ptNext[0],ptNext[1]);
                                pl.AddVertexAt(index-1,ptCurrent,0,width,width);
                                pl.ColorIndex = colorIndex;
                                pl.ConstantWidth = width;
                            }
                            trans.Commit();
                        }
                    }

                    index++;
                    ptPrevious = ptNext;
                    ppo.BasePoint = ptPrevious;
                }

                ppr = ed.GetPoint(ppo);

            }
        }

        private short GetColorIndex(Editor ed)
        {
            short colorIndex = 0;
            PromptIntegerOptions pio=new PromptIntegerOptions("");
            pio.Message = "\n请输入颜色索引值(0~256)";
            pio.AllowNegative = false;
            pio.DefaultValue = 0;
            PromptIntegerResult pir = ed.GetInteger(pio);
            if (pir.Status==PromptStatus.OK)
            {
                int t = pir.Value;
                if (t<257)
                    colorIndex = (short) t;
            }
            return colorIndex;
        }

        private double GetWidth(Editor ed) {
            double width = 0;
            PromptDoubleOptions pdo = new PromptDoubleOptions("");
            pdo.Message = "\n请输入线宽";
            pdo.AllowNegative = false;
            pdo.DefaultValue = 0;
            PromptDoubleResult pdr = ed.GetDouble(pdo);
            if (pdr.Status==PromptStatus.OK)
            {
                width = pdr.Value;
            }
            return width;
        }
    }
}
